package cn.com.cifi.mars.util;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * 
 * 字符串工具类
 */
public class NumberUtils {

	public static boolean isEmpty(final String s) {
		if (s == null) {
			return true;
		}
		return (s.isEmpty() || s.trim().length() == 0) || "null".equals(s);
	}

	public static boolean isNotEmpty(final String s) {
		return !isEmpty(s);
	}

	public static BigDecimal stringToBigDecimal(String str) {
		if (isEmpty(str)) {
			return null;
		}
		return new BigDecimal(str.trim());
	}

	public static Integer stringToInteger(String str) {
		if (isEmpty(str)) {
			return 0;
		}
		return Integer.valueOf(str.trim());
	}
	
	public static Integer intADD(Integer v1,Integer v2) {
		if(v1==null && v2 == null) {
			return null;
		}else {
			if(v1 == null) {
				v1 = 0;
			}
			if(v2 == null) {
				v2 =0;
			}
			return v1.intValue() + v2.intValue();
		}
	}
	
	public static BigDecimal BigDecimalADD(int scale,BigDecimal v1, BigDecimal v2) {
		if(v1 == null && v2 == null) {
			return null;
		}else {
			if(v1 == null) {
				v1 = BigDecimal.ZERO;
			}
			if(v2 == null) {
				v2 = BigDecimal.ZERO;
			}
			return v1.add(v2).setScale(scale, BigDecimal.ROUND_HALF_UP);
		}
	}

	public static BigDecimal BigDecimalADD(int scale,BigDecimal v, BigDecimal... arg) {
		if(v == null) {
			v = BigDecimal.ZERO;
		}
		BigDecimal sum = v;
		for (BigDecimal b : arg) {
			if(b == null) {
				b = BigDecimal.ZERO;
			}
	        sum = sum.add(b).setScale(scale, BigDecimal.ROUND_HALF_UP);;
	    }
	    return sum;
	}
	
	public static BigDecimal divide(BigDecimal v1, BigDecimal v2,int scale) {
		if (v1 == null || v2 == null) {
			return null;
		}
		if (v2.compareTo(BigDecimal.ZERO) == 0) {
			return null;
		}
		return v1.divide(v2, scale, BigDecimal.ROUND_HALF_UP);
	}

	public static String divide(String v1, String v2, int scale) {
		if (scale < 0 || isEmpty(v1) || isEmpty(v2)) {
			return "0.0000";
		}
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		if (b2.compareTo(new BigDecimal(0)) == 0) {
			return "0.0000";
		}
		return b1.divide(b2, scale, RoundingMode.HALF_UP).toString();
	}
	
	public static BigDecimal multiply(String v1, String v2, int scale) {
		if (scale < 0 || isEmpty(v1) || isEmpty(v2)) {
			return null;
		}
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
	}

	public static Integer IntegerSubtract(Integer v1, Integer v2) {
		if(v1 == null) {
			v1 = 0;
		}
		if(v2 == null) {
			v2 = 0;
		}
		return v1.intValue()-v2.intValue();
	}
	
	public static BigDecimal subtract(BigDecimal v1, BigDecimal v2) {
		if(v1 == null) {
			v1 = BigDecimal.ZERO;
		}
		if(v2 == null) {
			v2 = BigDecimal.ZERO;
		}
		return v1.subtract(v2);
	}
	
	public static int subtract(Integer v1, Integer v2) {
		if(v1 == null) {
			v1 = 0;
		}
		if(v2 == null) {
			v2 = 0;
		}
		return v1.intValue()-v2.intValue();
	}
	

	public static String fmtPriceMicrometer(String text,int scale) {
		if (text.indexOf(",") > 0) {
			text = text.replaceAll(",", "");
		}
		DecimalFormat df = new DecimalFormat("###,##0.000");
		if(scale == 0) {
			df = new DecimalFormat("###,##0");
		}else if(scale == 1) {
			df = new DecimalFormat("###,##0.0");
		}else if(scale == 2) {
			df = new DecimalFormat("###,##0.00");
		}
		double number = 0.0;
		try {
			number = Double.parseDouble(text);
		} catch (Exception e) {
			number = 0.0;
		}
		return df.format(number);
	}

	public static String fmtToWanUnit(String value) {
		String res = "";
		if (isEmpty(value)) {
			res = "0";
			return res;
		}
		if (value.indexOf(",") > 0) {
			value = value.replaceAll(",", "");
		}
		res = fmtPriceMicrometer(divide(value, "10000", 0),0);
		return res;
	}

	public static String fmtTo(BigDecimal value) {
		if (value == null){
			return "0";
		}
		double v1 = value.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
		return String.valueOf(v1);
	}

	public static String fmtToWan(String value) {
		String res = "";
		if (isEmpty(value)) {
			res = "0";
			return res;
		}
		if (value.indexOf(",") > 0) {
			value = value.replaceAll(",", "");
		}
		res = divide(value, "10000", 0);
		return res;
	}

	public static BigDecimal strToBigDecimal(String str) {
		if(isEmpty(str)) {
			return BigDecimal.ZERO;
		}else {
			if (str.indexOf(",") > 0) {
				str = str.replaceAll(",", "");
			}
			return new BigDecimal(str);
		}
	}
	public static boolean hasMoreOne(String str,String chars) {
		boolean flag = false;
		if(isEmpty(str)) {
			return flag;
		}
		String newStr = str.replace(chars, "");
		if(str.length() - newStr.length() >= 2) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 货值字符串初始化
	 * @param length 长度
	 * @param scale 精度
	 * @return
	 */
	public static String stringInitial(int length,int scale){
	    String str = "0.";
		if(scale==0){
		    str = "0";
		}
        for (int i = 1; i <= scale ; i++) {
            str+="0";
        }
        String s = str;
        for (int i = 1; i<length; i++){
            str +=" / " + s;
        }
		return str;
	}

    /**
     * 货值金额 根据是否车位楼栋 做加法
     * @param price 0 / 0 / 0
     * @param m 0
     * @param isParking
     * @return
     */
	public static String priceAddcalculate(String price,String m,int isParking){
        BigDecimal bigDecimal = new BigDecimal(m);
        if(bigDecimal.compareTo(BigDecimal.ZERO)==0){
            return price;
        }
        BigDecimal t1;
        BigDecimal t2 = new BigDecimal(price.substring(price.indexOf("/")+2, price.lastIndexOf("/")-1));
        BigDecimal t3 = new BigDecimal(price.substring(price.lastIndexOf("/")+2));

		if(isParking==0){
			t2=t2.add(bigDecimal);
		}else if(isParking==1) {
			t3=t3.add(bigDecimal);
		}
		t1=t2.add(t3);
		return t1.toString()+" / "+t2.toString()+" / "+t3.toString();
	}


    /**
	 * 货值金额 做加法
	 *
	 * @param a 0 / 0 / 0
	 * @param b 0 / 0 / 0
	 * @return
	 */
	public static String priceAdd(String a,String b){
		if(!a.contains("/") || !b.contains("/")){
			return stringInitial(3,4);
		}
		BigDecimal a1 = new BigDecimal(a.substring(0, a.indexOf("/")-1));
		BigDecimal b1 = new BigDecimal(b.substring(0, b.indexOf("/")-1));
		if(a1.compareTo(BigDecimal.ZERO)==0 && b1.compareTo(BigDecimal.ZERO)==0){
			return stringInitial(3,4);
		}
		BigDecimal a2 = new BigDecimal(a.substring(a.indexOf("/")+2, a.lastIndexOf("/")-1));
		BigDecimal a3 = new BigDecimal(a.substring(a.lastIndexOf("/")+2));

		BigDecimal b2 = new BigDecimal(b.substring(b.indexOf("/")+2, b.lastIndexOf("/")-1));
		BigDecimal b3 = new BigDecimal(b.substring(b.lastIndexOf("/")+2));

		BigDecimal p1=a1.add(b1);
		BigDecimal p2=a2.add(b2);
		BigDecimal p3=a3.add(b3);
		return p1.toString()+" / "+p2.toString()+" / "+p3.toString();
	}


	/**
	 * 货值金额计算均价
	 * @param price
	 * @param area
	 * @param num
	 * @return
	 */
	public static String priceDiv(String price,String area,String num){
		String d1;
		String d2;

		BigDecimal housePrice = new BigDecimal(price.substring(price.indexOf("/")+2, price.lastIndexOf("/")-1));
		BigDecimal parkingPrice = new BigDecimal(price.substring(price.lastIndexOf("/")+2));

		if(housePrice.compareTo(BigDecimal.ZERO) == 0 && parkingPrice.compareTo(BigDecimal.ZERO) == 0){
			return stringInitial(2,4);
		}
		BigDecimal houseArea = new BigDecimal(area);
		if(houseArea.compareTo(BigDecimal.ZERO)!=0){
			d1=BigDecimalUtils.divBigDecimal(housePrice,houseArea,0).toString();
		}else {
			d1 = "0.0000";
		}
		BigDecimal parkingNum = new BigDecimal(num.substring(num.lastIndexOf("/")+2));
		if(parkingNum.compareTo(BigDecimal.ZERO)!=0){
			d2 = BigDecimalUtils.divBigDecimal(parkingPrice,parkingNum,0).toString();
		}else {
			d2 = "0.0000";

		}
		return d1+" / "+d2;
	}

    /**
     * 货值金额计算百分率
     */
    public static String pricePercentage(String a, String b){

        BigDecimal a1 = new BigDecimal(a.substring(0, a.indexOf("/")-1));
        BigDecimal b1 = new BigDecimal(b.substring(0, b.indexOf("/")-1));
        if(a1.compareTo(BigDecimal.ZERO)==0 || b1.compareTo(BigDecimal.ZERO)==0){
            return "0.0%";
        }
		String text = a1.divide(b1, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100L)).setScale(1, RoundingMode.HALF_UP).toString();
		return NumberUtils.fmtPriceMicrometer(text,1)+"%";

    }

	/**
	 * 金额单位及格式转换
	 * 10000000/5000000/5000000  转1,000/500/500
	 */
	public static String priceToWanAndMicrometer(String price){
		if(StringUtils.isNotEmpty(price)){
			if(price.contains("/")){
				price = price.replace(",", "");
				String[] split = price.split(" / ");
				if(split.length==3){
					String t1 = split[0];
					String t2 = split[1];
					String t3 = split[2];
					return  fmtToWanUnit(t1)+" / "+fmtToWanUnit(t2)+" / "+fmtToWanUnit(t3);
				}
			}
			return fmtToWanUnit(price);
		}
		return "0";
	}

	/**
	 * 修改精度
	 * @param str 0.000
	 * @param scale 保留位数
	 * @return
	 */
	public static BigDecimal accuracyBigDecimal(String str,int scale){
		return strToBigDecimal(str).setScale(scale,RoundingMode.HALF_UP);
	}

	/**
	 * 修改精度
	 * @param str 0.000
	 * @param scale 保留位数
	 * @return
	 */
	public static String accuracyBigDecimalToStr(String str,int scale){
		return strToBigDecimal(str).setScale(scale,RoundingMode.HALF_UP).toString();
	}

	/**
	 * 修改精度
	 * @param str
	 * @param scale 保留位数
	 * @return
	 */
	public static String updateBigAccuracy(String str,int scale){
		if(StringUtils.isNotEmpty(str)){
			str = str.replace(",", "");
			if(str.contains("/")){
				String[] split = str.split(" / ");
				if(split.length==2){
					BigDecimal a1 = new BigDecimal(split[0]);
					BigDecimal a2 = new BigDecimal(split[1]);
					BigDecimal p1=a1.setScale(scale,RoundingMode.HALF_UP);
					BigDecimal p2=a2.setScale(scale,RoundingMode.HALF_UP);
					return fmtPriceMicrometer(p1.toString(),scale)+" / "+fmtPriceMicrometer(p2.toString(),scale);
				}else if(split.length==3){
					BigDecimal a1 = new BigDecimal(split[0]);
					BigDecimal a2 = new BigDecimal(split[1]);
					BigDecimal a3 = new BigDecimal(split[2]);

					BigDecimal p1=a1.setScale(scale,RoundingMode.HALF_UP);
					BigDecimal p2=a2.setScale(scale,RoundingMode.HALF_UP);
					BigDecimal p3=a3.setScale(scale,RoundingMode.HALF_UP);
					return fmtPriceMicrometer(p1.toString(),scale)+" / " + fmtPriceMicrometer(p2.toString(),scale)+" / "+ fmtPriceMicrometer(p3.toString(),scale);
				}
			}else {
				BigDecimal bigDecimal = new BigDecimal(str).setScale(scale, RoundingMode.HALF_UP);
				return fmtPriceMicrometer(bigDecimal.toString(),scale);
			}
		}
		return null;
	}

	/**
	 * 套数 根据是否车位楼栋 做加法
	 * @param num 0 / 0
	 * @param n 0
	 * @param isParking 是否车位楼栋
	 * @return
	 */
	public static String numAddcalculate(String num,String n,int isParking){
		BigDecimal bigDecimal = new BigDecimal(n);
		if(bigDecimal.compareTo(BigDecimal.ZERO)==0){
			return num;
		}
		BigDecimal t1 = new BigDecimal(num.substring(0, num.indexOf("/")-1));
		BigDecimal t2 = new BigDecimal(num.substring(num.lastIndexOf("/")+2));

		if(isParking == 0){
			t1 = t1.add(bigDecimal);
		}else if(isParking == 1) {
			t2 = t2.add(bigDecimal);
		}
		return t1.toString()+" / "+t2.toString();
	}


	/**
	 * 套数 做加法
	 * @param a 0 / 0
	 * @param b 0 / 0
	 * @return
	 */
	public static String numAdd(String a,String b){
		if(!a.contains("/") || !b.contains("/")){
			return stringInitial(2,5);
		}
		BigDecimal a1 = new BigDecimal(a.substring(0, a.indexOf("/")-1));
		BigDecimal b1 = new BigDecimal(b.substring(0, b.indexOf("/")-1));
		BigDecimal a2 = new BigDecimal(a.substring(a.lastIndexOf("/")+2));
		BigDecimal b2 = new BigDecimal(b.substring(b.lastIndexOf("/")+2));

		BigDecimal p1=a1.add(b1);
		BigDecimal p2=a2.add(b2);
		return p1.toString()+" / "+p2.toString();
	}
	
	public static BigDecimal strToDecimal(String str) {
		if(isEmpty(str)) {
			return null;
		}else {
			try {
				return new BigDecimal(str);
			} catch (Exception e) {
				return null;
			}
		}
	}
	
	public static String decimalToStr(BigDecimal val) {
		if(val== null) {
			return "";
		}else {
			try {
				return String.valueOf(val);
			} catch (Exception e) {
				return "";
			}
		}
	}
	
}
